/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package fr.ove.openmath.mathematica;

/**
 *
 * @author Mr.Sam
 */
import java.io.*;
import fr.inria.openmath.omapi.*;
import fr.inria.openmath.omapi.implementation.*;
import fr.ove.openmath.mfd2.OpenMathResultSetable;
import fr.ove.openmath.mathematica.omparser.PipeParserHandler;

public class Selector extends Thread {
    /**
    * The stream to read from.
    */
    private InputStream inputStream;
    
    /**
    * The object we will set the OpenMath object received.
    */
    private OpenMathResultSetable result;
    
    /**
    * The XML parser.
    */
    private XMLParser parser;
    
    /**
    * The handler for the parser.
    */
    private PipeParserHandler handler = new PipeParserHandler();
    
    /**
    * The Constructor.
    */
    public Selector(InputStream inputStream) {
        this.inputStream = inputStream;
        this.result = result;
        parser = new XMLParser(handler);
    }
    
    /**
    * Sets the object the will be set the OpenMath object received.
    */
    public void setOpenMathResultSetable(OpenMathResultSetable result) {
        this.result = result;
    }
    
    /**
    * Runs the selector.
    */
    public void run() {
        while (true) {
            try {
                parser.initParse(inputStream);
                    
                int dispo = 0;
                while ((dispo = inputStream.available()) == 0) {
                    try {
                        Thread.sleep(100);
                    }
                    catch (Exception e) {
                    }
                }
                    
                parser.parseObject(inputStream);
                    
                result.setOpenMathResult(handler.getOmObjectParsed());
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}